package cn.clickmed.DaTong;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Xml;

public class StringXmlUtil {
	private static XmlPullParser parser = Xml.newPullParser();

	/**
	 * 获取印章xml转换
	 * 
	 * @param xml
	 * @return
	 */
	public static List<Map<String, String>> parseGetSeals(String xml) {
		if (xml == null || xml.length() == 0)
			return null;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			parser.setInput(new StringReader(xml));
			int event = parser.getEventType();
			Map<String, String> seal = null;
			while (event != XmlPullParser.END_DOCUMENT) {// 没读到文档结束，就继续循环
				switch (event) {
				case XmlPullParser.START_DOCUMENT:// 文档开始

					break;
				case XmlPullParser.START_TAG:// 开始标签
					if ("hasError".equals(parser.getName().trim())) {
						if ("1".equals(parser.nextText().trim())) {// 1有错误
							return null;
						}
					} else if ("seal".equals(parser.getName().trim())) {
						seal = new HashMap<String, String>();
					} else if ("sealId".equals(parser.getName().trim())) {
						seal.put("sealId", parser.nextText());
					} else if ("sealName".equals(parser.getName().trim())) {
						seal.put("sealName", parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:// 结束标签
					if ("seal".equals(parser.getName().trim())) {
						list.add(seal);
						seal = null;
					}
					break;
				default:
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	/**
	 * 盖章Xml转换
	 */
	public static HashMap<String, String> parseAddSeal(String xml) {
		HashMap<String, String> map = null;
		try {
			parser.setInput(new StringReader(xml));
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {// 没读到文档结束，就继续循环
				switch (event) {
				case XmlPullParser.START_DOCUMENT:// 文档开始
					break;
				case XmlPullParser.START_TAG:// 开始标签
					if ("hasError".equals(parser.getName().trim())) {
						if ("1".equals(parser.nextText().trim())) {// 1有错误
							return null;
						}
					} else if ("sealRes".equals(parser.getName().trim())) {// 将签名值提取出来
						map = new HashMap<String, String>();
						map.put("sealRes", parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:// 结束标签
					break;
				default:
					break;
				}
				if (map != null)
					break;
				event = parser.next();

			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return map;
	}

	/**
	 * 验章xml转换
	 * 
	 * @param xml
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> parseVerifySeal(String xml) {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = null;
		try {
			parser.setInput(new StringReader(xml));
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {// 没读到文档结束，就继续循环
				switch (event) {
				case XmlPullParser.START_DOCUMENT:// 文档开始
					break;
				case XmlPullParser.START_TAG:// 开始标签
					if ("Seal".equals(parser.getName().trim())) {
						map = new HashMap<String, String>();
					} else if ("cert_id".equals(parser.getName().trim())) {
						map.put("cert_id", parser.nextText().trim());
					} else if ("seal_x".equals(parser.getName().trim())) {
						map.put("seal_x", parser.nextText().trim());
					} else if ("seal_y".equals(parser.getName().trim())) {
						map.put("seal_y", parser.nextText().trim());
					} else if ("user_id".equals(parser.getName().trim())) {
						map.put("user_id", parser.nextText().trim());
					} else if ("seal_id".equals(parser.getName().trim())) {
						map.put("seal_id", parser.nextText().trim());
					} else if ("seal_name".equals(parser.getName().trim())) {
						map.put("seal_name", parser.nextText().trim());
					} else if ("seal_time".equals(parser.getName().trim())) {
						map.put("seal_time", parser.nextText().trim());
					} else if ("seal_position".equals(parser.getName().trim())) {
						map.put("seal_position", parser.nextText().trim());
					} else if ("seal_picture".equals(parser.getName().trim())) {
						map.put("seal_picture", parser.nextText().trim());
					} else if ("verifyRes".equals(parser.getName().trim())) {
						map.put("verifyRes", parser.nextText().trim());
					} else if ("errorMsg".equals(parser.getName().trim())) {
						map.put("errorMsg", parser.nextText().trim());
					}
					break;
				case XmlPullParser.END_TAG:// 结束标签
					if ("Seal".equals(parser.getName().trim())) {
						list.add(map);
						map = null;
					}
					break;
				default:
					break;
				}
				event = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	public static Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}
}
